package com.jxc.controller;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;

import com.jxc.mapper.ShopMapper;
import com.jxc.mapper.UserMapper;
import com.jxc.po.Shops;
import com.jxc.po.User;
import com.jxc.util.DBUtil;
import java.net.URLDecoder;


@Controller
public class ShopManagerController  {
	  @RequestMapping(value="/getshoplist")
	  //public void login(PrintWriter out) {  
	  public void login(PrintWriter out) {  
		SqlSession sqlSession = null;
		Shops rs=new Shops();
		JSONObject jsonobject =null;	
		try {
			sqlSession = DBUtil.getSession();
			ShopMapper shopMapper = sqlSession.getMapper(ShopMapper.class);
			List<Shops> list=new ArrayList<Shops>();
			
			list=shopMapper.selectA();
			System.out.println("sdfsaf"+list.size());
			Map map = new HashMap();    
			JSONArray jsonArray = JSONArray.fromObject(list);
			if(list==null){				  
				 map.put( "result", "null");       				 
				 jsonobject = JSONObject.fromObject( map ); 
			}else{
				 map.put( "result", jsonArray);       				 
				 jsonobject = JSONObject.fromObject( map ); 
			}
			sqlSession.commit();
		} catch (Exception e) {
			System.out.println("sdfsaf"+e.toString());
			sqlSession.rollback();
		} finally {
			DBUtil.closeSession(sqlSession);
		}
		System.out.println(jsonobject);
		out.print(jsonobject);
	  }
	  @RequestMapping(value="/deleteshop")
	  //public void login(PrintWriter out) {  
	  public void delete(int id,PrintWriter out) {  
		SqlSession sqlSession = null;
		System.out.println("sdfsaf"+id);
		Shops rs=new Shops();
		JSONObject jsonobject =null;	
		try {
			sqlSession = DBUtil.getSession();
			ShopMapper shopMapper = sqlSession.getMapper(ShopMapper.class);
			shopMapper.delete(id);
			Map map = new HashMap();    
			map.put( "result", "success");  
			jsonobject = JSONObject.fromObject( map ); 
			sqlSession.commit();
		} catch (Exception e) {
			System.out.println("sdfsaf"+e.toString());
			sqlSession.rollback();
		} finally {
			DBUtil.closeSession(sqlSession);
		}
		System.out.println(jsonobject);
		out.print(jsonobject);
	  }
	  @RequestMapping(value="/addshop")
	  //public void login(PrintWriter out) {  
	  public void add(Shops shop,PrintWriter out) {  
		SqlSession sqlSession = null;
		JSONObject jsonobject =null;	
		try {
			sqlSession = DBUtil.getSession();
			ShopMapper shopMapper = sqlSession.getMapper(ShopMapper.class);
			Map map = new HashMap();
			Shops s=shopMapper.select(shop);
			if(s!=null){
				map.put( "result", 1);       				 
				jsonobject = JSONObject.fromObject( map ); 
			}else{
				map.put( "result", 0); 
				shopMapper.insert(shop);
				jsonobject = JSONObject.fromObject( map ); 
			}
			sqlSession.commit();
		} catch (Exception e) {
			System.out.println("sdfsaf"+e.toString());
			sqlSession.rollback();
		} finally {
			DBUtil.closeSession(sqlSession);
		}
		System.out.println(jsonobject);
		out.print(jsonobject);
	  }
	  @RequestMapping(value="/updateshop")
	  //public void login(PrintWriter out) {  
	  public void update(Shops shop,PrintWriter out) {  
		SqlSession sqlSession = null;
		JSONObject jsonobject =null;	
		try {
			sqlSession = DBUtil.getSession();
			ShopMapper shopMapper = sqlSession.getMapper(ShopMapper.class);
			
			Map map = new HashMap();
			Shops s=shopMapper.select(shop);
			if(""==null){				  
				 map.put( "result", 1);       				 
				 jsonobject = JSONObject.fromObject( map ); 
			}else{
				 map.put( "result", 0); 
				 shopMapper.update(shop);
				 jsonobject = JSONObject.fromObject( map ); 
			}
			sqlSession.commit();
		} catch (Exception e) {
			System.out.println("sdfsaf"+e.toString());
			sqlSession.rollback();
		} finally {
			DBUtil.closeSession(sqlSession);
		}
		System.out.println(jsonobject);
		out.print(jsonobject);
	  }
	
}
